home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / src / calendar.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-03  |  4.7 KB  |  178 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: calendar.c,v 5.3 1993/02/03 19:06:31 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.3 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1988-1992 USENET Community Trust
  8.  *             Copyright (c) 1986,1987 Dave Taylor
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log: calendar.c,v $
  17.  * Revision 5.3  1993/02/03  19:06:31  syd
  18.  * Remove extra strchr/strcat/strcpy et al declarations
  19.  * From: Syd
  20.  *
  21.  * Revision 5.2  1992/11/26  00:46:50  syd
  22.  * Fix how errno is used so err is inited and used instead
  23.  * as errno gets overwritten by print system call
  24.  * From: Syd
  25.  *
  26.  * Revision 5.1  1992/10/03  22:58:40  syd
  27.  * Initial checkin as of 2.4 Release at PL0
  28.  *
  29.  *
  30.  ******************************************************************************/
  31.  
  32. /** This routine implements a rather snazzy idea suggested by Warren
  33.     Carithers of the Rochester Institute of Technology that allows
  34.     mail to contain entries formatted in a manner that will allow direct
  35.     copying into a users calendar program.
  36.  
  37.     All lines in the current message beginning with "->", e.g.
  38.  
  39.     -> Mon 04/21 1:00p meet with chairman candidate
  40.  
  41.     get copied into the user's calendar file.
  42.  
  43. **/
  44.  
  45. #include "headers.h"
  46.  
  47. #ifdef ENABLE_CALENDAR        /* if not defined, this will be an empty file */
  48.  
  49. #include <errno.h>
  50. #include <s_error.h>
  51.  
  52. extern int errno;
  53.  
  54. char *error_description();
  55.  
  56. scan_calendar()
  57. {
  58.     FILE *calendar;
  59.     int  count;
  60.     int  err;
  61.  
  62.     /* First step is to open the calendar file for appending... **/
  63.  
  64.     if (can_open(calendar_file, "a") != 0) {
  65.       err = errno;
  66.       dprint(2, (debugfile,
  67.           "Error: wrong permissions to append to calendar %s\n",
  68.           calendar_file));
  69.       dprint(2, (debugfile, "** - %s **\n", error_description(err)));
  70.       error1(catgets(elm_msg_cat, ErrorSet, ErrorCalendarCanOpen,
  71.           "Not able to append to file %s!"), calendar_file);
  72.       return; 
  73.     }
  74.  
  75.     save_file_stats(calendar_file);
  76.  
  77.     if ((calendar = fopen(calendar_file,"a")) == NULL) {
  78.       err = errno;
  79.       dprint(2, (debugfile, 
  80.         "Error: couldn't append to calendar file %s (scan)\n", 
  81.         calendar_file));
  82.       dprint(2, (debugfile, "** - %s **\n", error_description(err)));
  83.       error1(catgets(elm_msg_cat, ErrorSet, ErrorCalendarAppend,
  84.           "Couldn't append to file %s!"), calendar_file);
  85.       return; 
  86.     }
  87.     
  88.     count = extract_info(calendar);
  89.  
  90.     fclose(calendar);
  91.  
  92.     restore_file_stats(calendar_file);
  93.  
  94.     if (count > 0) {
  95.       if (count > 1)
  96.         error1(catgets(elm_msg_cat, ErrorSet, ErrorCalendarSavedPlural,
  97.             "%d entries saved in calendar file."), count);
  98.       else
  99.         error(catgets(elm_msg_cat, ErrorSet, ErrorCalendarSaved,
  100.             "1 entry saved in calendar file."));
  101.     } else 
  102.       error(catgets(elm_msg_cat, ErrorSet, ErrorCalendarNoneSaved,
  103.           "No calendar entries found in that message."));
  104.  
  105.     return;
  106. }
  107.  
  108. int
  109. extract_info(save_to_fd)
  110. FILE *save_to_fd;
  111. {
  112.     /** Save the relevant parts of the current message to the given
  113.         calendar file.  The only parameter is an opened file
  114.         descriptor, positioned at the end of the existing file **/
  115.         
  116.     register int entries = 0, lines;
  117.     int line_length;
  118.     char buffer[SLEN], *cp, *is_cal_entry();
  119.  
  120.         /** get to the first line of the message desired **/
  121.  
  122.         if (fseek(mailfile, headers[current-1]->offset, 0) == -1) {
  123.              dprint(1,(debugfile, 
  124.         "ERROR: Attempt to seek %d bytes into file failed (%s)",
  125.         headers[current-1]->offset, "extract_info"));
  126.              error1(catgets(elm_msg_cat, ErrorSet, ErrorCalendarSeek,
  127.           "ELM [seek] failed trying to read %d bytes into file."),
  128.              headers[current-1]->offset);
  129.              return(0);
  130.         }
  131.  
  132.         /* how many lines in message? */
  133.  
  134.         lines = headers[current-1]->lines;
  135.  
  136.         /* now while not EOF & still in message... scan it! */
  137.  
  138.     while (lines) {
  139.  
  140.           if((line_length = mail_gets(buffer, SLEN, mailfile)) == 0)
  141.         break;
  142.  
  143.       if(buffer[line_length - 1] == '\n')
  144.         lines--;                    /* got a full line */
  145.  
  146.       if((cp = is_cal_entry(buffer)) != NULL) {
  147.         entries++;
  148.         fprintf(save_to_fd,"%s", cp);
  149.       }
  150.  
  151.     }
  152.     dprint(4,(debugfile,
  153.         "Got %d calender entr%s.\n", entries, entries > 1? "ies":"y"));
  154.  
  155.     return(entries);
  156. }
  157.  
  158. char *
  159. is_cal_entry(string)
  160. register char *string;
  161. {
  162.     /* If string is of the form
  163.          * {optional white space} ->{optional white space} {stuff}
  164.      * return a pointer to stuff, otherwise return NULL.
  165.      */
  166.     while( whitespace(*string) )
  167.       string++;      /* strip leading W/S */
  168.     
  169.     if(strncmp(string, "->", 2) == 0) {
  170.       for(string +=2 ; whitespace(*string); string++)
  171.           ;
  172.       return(string);
  173.     }
  174.     return(NULL);
  175. }
  176.  
  177. #endif
  178.